/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package famtime.tm.kalender.business;

import famtime.tm.kalender.model.Event;
import famtime.tm.kalender.model.FamUser;
import famtime.tm.kalender.model.Familie;
import famtime.tm.kalender.model.Kalender;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Marius
 */
@Stateless
public class KalenderService {

    @PersistenceContext
    private EntityManager em;

    public void create(Kalender k) {
        em.persist(k);
    }
    
    /** Registrierung **/
    public void register(FamUser user){
        em.persist(user);
    }
    
    public void register(Familie familie)
    {
        em.persist(familie);
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public EntityManager getEm() {
        return em;
    }

    public void update(Kalender k) {
        em.merge(k);
    }

    public void remove(Kalender k) {
        em.remove(k);
    }

   /* public Kalender findByName(String name) throws NoSuchKalenderException {
        List<Kalender> resultList = em.createNamedQuery(Kalender.FIND_BY_NAME, Kalender.class)
                .setParameter("name", name)
                .getResultList();
        if (resultList != null && resultList.size() > 1) {
            throw new IllegalStateException("kalender named " + name + " was found more than once.");
        }
        if (resultList != null && resultList.size() == 1) {
            return resultList.get(0);
        }
        throw new NoSuchKalenderException("kalender " + name + " doesn't exist.");
    }*/
    
    public Kalender removeEvent(Kalender k, Event e) {
        k = em.merge(k);
        k.remove(e);
        return k;
    }
   
    
    public List<Kalender> getAll() {
        TypedQuery<Kalender> query = em.createQuery("select c from Kalender c", Kalender.class);

        return query.getResultList();
    }

    public Kalender addEvent(String titel, String desc, Date startDate, Date endDate, Kalender k) {
        k = em.merge(k);
        Event e = new Event(titel, desc, startDate, endDate);
        em.persist(e);
        k.addEvent(e);
        return k;
    }
    
    public Kalender addEvent1(String titel, String desc, Date start, Date end, Kalender k) {
        k = em.merge(k);
        Event e = new Event(titel, desc, start, end);
        em.persist(e);
        k.addEvent(e);
        return k;
    }
        
    public Event removeEvent(long id, Kalender k)
    {
        List<Event> toRemove = findEventsInKalender(k);
        
        for(Event e: toRemove){
            if(e.getId() == id){                        
                em.remove(e);
                k.remove(e);
                return e;
            }
        }
        return null;
    }

  /*  public List<Kalender> findAll() {
        return em.createNamedQuery(Kalender.FIND_ALL, Kalender.class).getResultList();
    }*/
    public FamUser addUser(String username, String password){
        FamUser f = new FamUser(username, password);
        return f;
    }
    
    public FamUser login(String username, String password) {
        List<FamUser> resultList;
        resultList = em.createQuery("select k from FamUser k where k.username=:username and k.password=:password")
                .setParameter("username", username)
                .setParameter("password", password)
                .getResultList();

        if(resultList.isEmpty()) {
            return null;
        }
        else {
            return resultList.get(0);
        }
    }

    public List<Event> findEventsInKalender(Kalender k)
    {
         List<Event> resultList;
        resultList = em.createQuery("select k from Event k where k.parent=:kalender")
                .setParameter("kalender", k)
                .getResultList();
        
        return resultList;
    }
    
    
    public List<FamUser> findUserInFamilie(Familie f)
    {   
        List<FamUser> famList = em.createQuery("select f from FamUser f where f.parent=:familie")
                .setParameter("familie", f)
                .getResultList();
        
        return famList;
    }
    
    public List<Kalender> findKalenderInFamilie(List<FamUser> famList){
        List<Kalender> kalenderList;
        kalenderList = new ArrayList<Kalender>();
        for(FamUser fu: famList){
            kalenderList.add(fu.getKalender());
        }
        return kalenderList;        
    }
    
    public List<Event> findEventsInFamilie(List<Kalender> kalenderList){
        List<Event> resultList;
        resultList = new ArrayList<Event>();
        for(Kalender kal : kalenderList) {
            resultList.addAll(findEventsInKalender(kal));
        }        
        return resultList;
    }
    
    public boolean addUserToFamilie(FamUser newUser, String familienName) {
       List<Familie> resultList;
        resultList = em.createQuery("select k from Familie k where k.name=:familienName")
                .setParameter("familienName", familienName)
                .getResultList();

        if(resultList.isEmpty()) {
            return false;
        }
        else {
            System.out.println("BLAB123ALB");
            Familie m = em.merge(resultList.get(0));
        //    m.addMember(newUser);
            System.out.println("BLAB123ALB USER ADDED");
           newUser = em.merge(newUser);
            
            newUser.setParent(m);
             System.out.println("BLAB123ALB PARENT SET");
            
            return true;
        }
    }   
}
